# ../addons/manilib/core/clients/flags/__init__.py

'''
$Rev: 8 $
$Author: pitkley $
$Date: 2009-11-01 17:54:55 +0000 (Sun, 01 Nov 2009) $
'''

# ==============================================================================
# >> IMPORTS
# ==============================================================================
# Manilib Imports
from core import ManilibError
from core.commands import Command
from manilib.core.clients.client import _add_client
from core.clients.client import _is_client

# ==============================================================================
# >> GLOBALS
# ==============================================================================
list_adminFlags = {"a":"Private Admin Chat", "A":"Accept Vote",
    "admin":"Basic Admin", "b":"non-permanent ban", "B":"RCON voting menu",
    "c":"Map Change", "C":"Cancel vote", "client":"Create Client and Set Flags",
    "d":"Drug", "D":"Menu Question Vote", "e":"Explode",
    "E":"View client rates", "f":"Freeze", "F":"Burn", "g":"Swap Player",
    "G":"No Clip Mode", "grav":"Adjust Player Gravity", "H":"War Mode",
    "i":"Blind", "I":"Mute", "j":"Gimp", "J":"Reset Stats", "k":"Kick",
    "K":"Cash", "l":"Slap", "L":"RCON Say", "m":"Slay", "M":"Admin Skins",
    "N":"Set skins", "o":"Admin Chat", "O":"Drop C4", "p":"Sound",
    "P":"Set Client Flags", "pban":"Permanent Ban", "q":"RCON menu level 1",
    "Q":"Console Question Vote", "q2":"RCON menu level 2",
    "q3":"RCON menu level 3", "r":"RCON", "R":"RCON Voting",
    "reserved":"Reserved Slot", "s":"Admin Say", "S":"Set skin color",
    "spray":"Spraytag tracking", "t":"Teleport", "T":"Time Bomb",
    "U":"Fire Bomb", "v":"Random map vote", "V":"Map vote",
    "w":"Weapon restrictions", "W":"Freeze Bomb", "x":"Client Execution",
    "X":"Adjust Health", "y":"Client execution menu", "Y":"Beacon player",
    "z":"Config", "Z":"Give Item"}

list_immunityFlags = {"a":"Name tag kick", "afk":"AFK kick",
    "autojoin":"Autojoin", "b":"Ban", "c":"Balance team", "d":"Drug",
    "e":"Burn", "f":"Freeze", "g":"Swap", "grav":"Per player gravity",
    "h":"Mute", "i":"Blind", "immunity":"Basic", "j":"Gimp", "k":"Kick",
    "l":"Slap", "m":"Slay", "n":"Reserved slot kick", "o":"Set skin",
    "p":"Reserved Skin", "ping":"Ping kick", "q":"Timebomb", "r":"Firebomb",
    "s":"Freezebomb", "t":"Teleport", "u":"Beacon", "v":"Ghost",
    "w":"Give item", "x":"Client execution", "y":"Color change"}

# ==============================================================================
# >> CLASSES
# ==============================================================================
class AdminFlagsList(list):
    def __init__(self, id):
        self.id = id

    def append(self, item):
        # Check if the client exists
        if not isClient(self.id):
            raise ManilibError('Unable to get client with id "%s". ' % self.id +
                'No such client.')

        # Split the flags up
        flags = list(item)

        # Check if flags exist and is not appended yet, otherwise abort the
        #   complete action (even correct flags won't get added)!
        for flag in flags:
            if not flag in list_adminFlags:
                raise ManilibError('Unable to append flag "%s": Incorrect flag'
                    % flag)
            elif flag in self:
                raise KeyError('Unable to append flag "%s": Already appended')

        # Append the flag/s
        for flag in flags:
            super(AdminFlagsList, self).append(flag)

        # Run the ma_client SetAFlag command to update the real mani client
        #   (no for-loop needed, mani accepts the whole flagstring)
        Command.client('SetAFlag', '"%s"' % self.id, '"+%s"' % item)

    def remove(self, item):
        # Check if the client exists
        if not isClient(self.id):
            raise ManilibError('Unable to get client with id "%s". ' % self.id +
                'No such client.')

        # Split the flags up
        flags = list(item)

        # Check if flags exist and is appended, if not, abort the complete
        #   action (even correct flags won't get removed)!
        for flag in flags:
            if not flag in list_adminFlags:
                raise ManilibError('Unable to remove flag "%s": Incorrect flag'
                    % flag)
            elif not flag in self:
                raise KeyError('Unable to remove flag "%s": No such flag')

        # Remove the flag/s
        for flag in flags:
            super(AdminFlagsList, self).remove(flag)

        # Run the ma_client SetAFlag command to update the real mani client
        #   (no for-loop needed, mani accepts the whole flagstring)
        Command.client("SetAFlag", '"%s"' % self.id, "-%s" % item)



class ImmunityFlagsList(list):
    pass